package com.example.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Service.impl.SupplierServiceImpl;
import com.example.pojo.Supplier;
import com.example.util.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

@Api(tags = "供应商接口")
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierServiceImpl supplierService;

    @GetMapping("/getList")
    public Object  getList(@RequestParam(value = "SupplierName",defaultValue = "")String SupplierName,
                           @RequestParam(value = "page",defaultValue = "1") int page,
                           @RequestParam(value = "size",defaultValue = "5") int size){
        HashMap<String,Object> map = new HashMap<>();
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("supplier_name",SupplierName);
        Page<Supplier> Page = new Page<>(page,size);
        Page<Supplier> supplierPage= supplierService.page(Page,queryWrapper);
        map.put("supplierPage",supplierPage.getRecords());
        map.put("total",supplierPage.getTotal());
        map.put("Pages",supplierPage.getPages());
        return new Result<>("200","查询成功",map);
    }

    @PostMapping("/add")
    public Object add(@RequestBody Supplier supplier){
        return new Result<>("200","添加成功",supplierService.save(supplier));
    }

    @DeleteMapping("/delete{id}")
    public Object delete(@PathVariable int id){
        return new Result<>("200","删除成功",supplierService.removeById(id));
    }

    @PutMapping("/update")
    public Object update(@RequestBody Supplier supplier){
        return new Result<>("200","修改成功",supplierService.updateById(supplier));
    }
}
